package com.changil.nowonchangilchurch;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.changil.nowonchangilchurch.common.BlurTask;
import com.changil.nowonchangilchurch.common.BlurTask.BlurTaskListener;
import com.changil.nowonchangilchurch.common.SleepTask;
import com.changil.nowonchangilchurch.common.SleepTask.SleepTaskListener;
import com.changil.nowonchangilchurch.page.info.InfoActivity;
import com.changil.nowonchangilchurch.page.mission.MissionActivity;
import com.changil.nowonchangilchurch.page.news.NewsActivity;
import com.changil.nowonchangilchurch.page.word.WordActivity;
import com.changil.nowonchangilchurch.ui.layout.IntroLayer;
import com.changil.nowonchangilchurch.ui.layout.MainViewFlipper;
import com.changil.nowonchangilchurch.util.AnimationUtil;

public class MainActivity extends Activity {
	private LinearLayout mRootLayout = null;
	private IntroLayer mIntroLayer = null;
	private ViewFlipper mViewFlipper = null;
	private ImageView mMainImg1 = null;
	private ImageView mMainImg2 = null;
	private ImageView mMainImg3 = null;
	private ImageView mMainImg4 = null;
	private ImageView mMainImg5 = null;
	private LinearLayout mInfo = null;
	private LinearLayout mWord = null;
	private LinearLayout mMission = null;
	private LinearLayout mNews = null;
	private ImageView mInfoBtn = null;
	
	private long mLastBackKeyClickTime = 0;
	private static final long mBackKeyDoubleClickInterval = 3000; // 3 초
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        createLayout();
        if(null == savedInstanceState) {
        	mIntroLayer = new IntroLayer(this);
        	mIntroLayer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
        	mRootLayout.addView(mIntroLayer,0);
        }
        initLayout();
    }
    
    
    
    @Override
	protected void onResume() {
		super.onResume();
    	mViewFlipper.startFlipping();
	}



	@Override
	protected void onStop() {
		super.onStop();
		mViewFlipper.stopFlipping();
	}
	
	private void createLayout() {
		mRootLayout = (LinearLayout)findViewById(R.id.activity_main_root_layout);
		
    	mViewFlipper = (MainViewFlipper)findViewById(R.id.activity_main_view_flipper);
    	mViewFlipper.setInAnimation(AnimationUtil.inFromRightAnimation());
		mViewFlipper.setOutAnimation(AnimationUtil.outToLeftAnimation());
		
		mMainImg1 = (ImageView)findViewById(R.id.activity_main_main_img1);
		mMainImg2 = (ImageView)findViewById(R.id.activity_main_main_img2);
		mMainImg3 = (ImageView)findViewById(R.id.activity_main_main_img3);
		mMainImg4 = (ImageView)findViewById(R.id.activity_main_main_img4);
//		mMainImg5 = (ImageView)findViewById(R.id.activity_main_main_img5);
		
//		mMainImg1.setImageBitmap(UiUtil.fastBlur(BitmapFactory.decodeResource(getResources(), R.drawable.church_day), 10));
//		mMainImg2.setImageBitmap(UiUtil.fastBlur(BitmapFactory.decodeResource(getResources(), R.drawable.church_night), 10));
//		mMainImg3.setImageBitmap(UiUtil.fastBlur(BitmapFactory.decodeResource(getResources(), R.drawable.church_cross1), 10));
//		mMainImg4.setImageBitmap(UiUtil.fastBlur(BitmapFactory.decodeResource(getResources(), R.drawable.church_cross2), 10));
//		mMainImg5.setImageBitmap(UiUtil.fastBlur(BitmapFactory.decodeResource(getResources(), R.drawable.church_cross3), 10));
    	
		mInfo = (LinearLayout)findViewById(R.id.activity_main_info);
    	mWord = (LinearLayout)findViewById(R.id.activity_main_word);
    	mMission = (LinearLayout)findViewById(R.id.activity_main_mission);
    	mNews = (LinearLayout)findViewById(R.id.activity_main_news);
    	
    	mInfo.setOnClickListener(mOnClickListener);
    	mWord.setOnClickListener(mOnClickListener);
    	mMission.setOnClickListener(mOnClickListener);
    	mNews.setOnClickListener(mOnClickListener);
    	
    	mInfoBtn = (ImageView)findViewById(R.id.activity_main_info_btn);
    	mInfoBtn.setOnClickListener(mOnClickListener);
    }
    
    private void initLayout() {
    	BlurTask task1 = BlurTask.newInstance(mMainImg1);
		task1.setBlurTaskListener(new BlurTaskListener() {
			
			@Override
			public void onBlurTaskCompleted() {
				SleepTask sleepTask = SleepTask.newInstance();
				sleepTask.setSleepTaskListener(new SleepTaskListener() {
					
					@Override
					public void onSleepTaskCompleted() {
						if(null != mIntroLayer) {
							mIntroLayer.setVisibility(View.GONE);
						}
					}
				});
				sleepTask.execute(500);
			}
		});
		task1.execute(BitmapFactory.decodeResource(getResources(), R.drawable.church_day));
		
		BlurTask task2 = BlurTask.newInstance(mMainImg2);
//		task2.setBlurTaskListener(new BlurTaskListener() {
//			
//			@Override
//			public void onBlurTaskCompleted() {
//				if(null != mIntroLayer) {
//					mIntroLayer.setVisibility(View.GONE);
//				}
//			}
//		});
		task2.execute(BitmapFactory.decodeResource(getResources(), R.drawable.church_night));
		
		BlurTask task3 = BlurTask.newInstance(mMainImg3);
//		task3.setBlurTaskListener(new BlurTaskListener() {
//			
//			@Override
//			public void onBlurTaskCompleted() {
//				if(null != mIntroLayer) {
//					mIntroLayer.setVisibility(View.GONE);
//				}
//			}
//		});
		task3.execute(BitmapFactory.decodeResource(getResources(), R.drawable.church_cross1));
		
		BlurTask task4 = BlurTask.newInstance(mMainImg4);
//		task4.setBlurTaskListener(new BlurTaskListener() {
//			
//			@Override
//			public void onBlurTaskCompleted() {
//				if(null != mIntroLayer) {
//					mIntroLayer.setVisibility(View.GONE);
//				}
//			}
//		});
		task4.execute(BitmapFactory.decodeResource(getResources(), R.drawable.church_cross2));
		
//		BlurTask task5 = BlurTask.newInstance(mMainImg5);
//		task5.setBlurTaskListener(new BlurTaskListener() {
//			
//			@Override
//			public void onBlurTaskCompleted() {
//				mIntroLayer.setVisibility(View.GONE);
//			}
//		});
//		task5.execute(BitmapFactory.decodeResource(getResources(), R.drawable.church_cross3));
    }
    
    private OnClickListener mOnClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			if(v.getId() == mInfo.getId()) {
				Intent i = new Intent(MainActivity.this, InfoActivity.class);
				startActivity(i);
			} else if(v.getId() == mWord.getId()) {
				Intent i = new Intent(MainActivity.this, WordActivity.class);
				startActivity(i);
			} else if(v.getId() == mMission.getId()) {
				Intent i = new Intent(MainActivity.this, MissionActivity.class);
				startActivity(i);
			} else if(v.getId() == mNews.getId()) {
				Intent i = new Intent(MainActivity.this, NewsActivity.class);
				startActivity(i);
			} else if(v.getId() == mInfoBtn.getId()) {
				AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
				alert.setTitle(getString(R.string.copyrights_info));
				alert.setPositiveButton(getString(R.string.common_confirm), new DialogInterface.OnClickListener() {
				    @Override
				    public void onClick(DialogInterface dialog, int which) {
				    dialog.dismiss();     //닫기
				    }
				});
				alert.setMessage(getString(R.string.opensource_license)+"\n\n" +
								getString(R.string.use_this_opensource)+"\n" +
								getString(R.string.opensource_list)+
								getString(R.string.use_apache_license)+"\n\n" +
								getString(R.string.apache_license));
				alert.show();
			}
			
		}
	};
	
	
	
	@Override
	public void onBackPressed() {
		long now = System.currentTimeMillis();
		if (now - mLastBackKeyClickTime < mBackKeyDoubleClickInterval) {
			super.onBackPressed();
		} else {
			Toast.makeText(this, getString(R.string.press_back_to_finish), Toast.LENGTH_SHORT).show();
			mLastBackKeyClickTime = now;
		}
	}



	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState = new Bundle();
	}
	
	
    
}
